﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NumericalData.Reflection;
using NumericalData.Systems;
using NumericalData;
using NumericalData.Statistics;
using Vector = NumericalData.Vector;

namespace WPFSolution.UserControls
{
    /// <summary>
    /// Interaction logic for AnswerDetails.xaml
    /// </summary>
    public partial class AnswerDetails : UserControl
    {
        public AnswerDetails()
        {
            InitializeComponent();
        }

        public void SetAnswer(DiagnosticsAnswer ans,bool showVector)
        {
            AnswerProperties.Children.Clear();
            var answerDict = AnswersReflection.GetNameValuesDictionary<DiagnosticsAnswer>(ans);
            foreach (KeyValuePair<string, string> pair in answerDict)
            {
                var container = new DockPanel();
                var label =
                    new Label{Width= 130, Content = pair.Key };
                var text =
                    new TextBox{ Text = pair.Value, IsEnabled = false };
                container.Children.Add(label);
                container.Children.Add(text);
                AnswerProperties.Children.Add(container);
            }
            if (showVector)
            {
                VectorPart.Visibility = Visibility.Visible;
                FillAnswerVector(ans.AnswerVector);
            }
            else
            {
                VectorPart.Visibility = Visibility.Collapsed;
            }
        }


        private TextBox CreateCell(string text, string tag)
        {
            return new TextBox
            {
                BorderThickness = new Thickness(2, 2, 2, 5),
                Padding = new Thickness(5, 5, 5, 5),
                Margin = new Thickness(10, 10, 10, 10),
                Width = 60,
                Tag = tag,
                Text = text,
                ToolTip = new ToolTip() { Content = tag }
            };
        }

        void FillAnswerVector(Vector vec)
        {
            if (AnswerVectorStackPanel != null)
            {
                AnswerVectorStackPanel.Children.Clear();
                System.GC.Collect();
                for (int i = 0; i < vec.Length; ++i)
                {
                    AnswerVectorStackPanel.Children.Add(CreateCell(vec[i].ToString(), i.ToString()));
                }
            }

        }
    }
}
